<?php

namespace App\Http\Controllers\Admin\Money;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Moneys;
use App\Models\Users;

/**
 * 订单管理控制器
 *
 * @author yh
 * @package App\Http\Controllers\Orders
 */
class OrdersController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 商家列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Orders::orderBy($sort, $order)->where('created_at','<',date("Y-m-d"));
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('nickname')){
                $query->where('nickname','LIKE', "%" . request('nickname') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'title',
                'nickname',
                'total',
                'title',
                'money',
                'num',
                'order_sn',
                'status',
                'created_at',
                'donate_avatar',
                'donate_nickname'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.orders', [
                'orders' => $shops,
            ]);
        }

        /**
         * 已完成列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function order_finish()
        {
            // 处理排序
            $sort = request('sort', 'id');  
            $order = request('order', 'desc');
            $query = Orders::orderBy($sort, $order)->where('status', 4);
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('nickname')){
                $query->where('nickname','LIKE', "%" . request('nickname') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'title',
                'nickname',
                'total',
                'title',
                'money',
                'order_sn',
                'num',
                'status',
                'created_at',
                'donate_avatar',
                'donate_nickname'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.order_finish', [
                'orders' => $shops,
            ]);
            
        }
        /**
         * 已完成列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function order_fail()
        {
            // 处理排序
            $sort = request('sort', 'id');  
            $order = request('order', 'desc');
            $query = Orders::orderBy($sort, $order)->where('status', '!=',4)->where('created_at','<',date("Y-m-d"));
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('nickname')){
                $query->where('nickname','LIKE', "%" . request('nickname') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'title',
                'nickname',
                'total',
                'title',
                'money',
                'num',
                'order_sn',
                'status',
                'created_at',
                'donate_avatar',
                'donate_nickname'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.order_fail', [
                'orders' => $shops,
            ]);
        }
       
        /**
         * 确认完成兑换商品订单
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public  function finish($id){
            $order = Orders::where('id', $id)->first();
            $user = Users::where('uid', $order['author_id'])->first();
            $total = $user->score + ($order->exchange_num) * ($order->score);
            $user->score = $user->score + ($order->exchange_num) * ($order->score);
            $user->save();
            $sign = New Signs();
            $sign->uid = $order->author_id;
            $sign->type = $order->type;
            $sign->add_type = 1;
            $sign->scores = $total;
            $sign->username = $user->username;
            $sign->save();
            $order->status = 3;
            $order->save();
            return $this->success();
        }
    /**
     * 删除用户
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Orders::destroy($id);
            return $this->success();
        }
    }
    }